package org.skydiveapp;

import org.skydiveapplibrary.R;
import org.skydiveapplibrary.data.MoreCategory;
import org.skydiveapplibrary.ui.MoreCategoryAdapter;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class MoreActivity extends BaseListFragment
{
	// activity request codes
	 private static final int REQUEST_IMPORT_EXPORT = 0;
	 
	// listeners
	private OnItemClickListener itemClickListener = new OnItemClickListener()
	{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id)
		{
			// get selected category
			MoreCategoryAdapter adapter = (MoreCategoryAdapter)getListAdapter();
			MoreCategory category = (MoreCategory)adapter.getItem(position);
	    	// handle selection
			handleSelection(category);
		}
	};
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
	    		Bundle savedInstanceState)
	{	
	    // Inflate the layout for this fragment
	    View view = inflater.inflate(R.layout.more_list_view, container, false);
		
		return view;
	}
	
	@Override
	public void onResume()
	{
		super.onResume();
		
		// set adapter
		setListAdapter(new MoreCategoryAdapter(getActivity()));
		
		// hide listview extras
		hideListviewExtras();
		
		getListView().setOnItemClickListener(itemClickListener);
	}
	
	private void handleSelection(MoreCategory category)
	{
		if (category == MoreCategory.Aircraft)
		{	
			Intent intent = new Intent(getActivity(), AircraftListActivity.class);
            startActivity(intent);
		}
		else if (category == MoreCategory.SkydiveTypes)
		{
			Intent intent = new Intent(getActivity(), SkydiveTypeListActivity.class);
            startActivity(intent);
		}
		else if (category == MoreCategory.Locations)
		{			
			Intent intent = new Intent(getActivity(), LocationListActivity.class);
            startActivity(intent);
		}
		else if (category == MoreCategory.WingLoadCalculator)
		{		
			Intent intent = new Intent(getActivity(), WingLoadCalculatorActivity.class);
            startActivity(intent);
		}
		else if (category == MoreCategory.ImportExport)
		{
			Intent intent = new Intent(getActivity(), ImportExportActivity.class);
            startActivityForResult(intent, REQUEST_IMPORT_EXPORT);
		}
		else if (category == MoreCategory.Settings)
		{
			Intent intent = new Intent(getActivity(), SettingsActivity.class);
            startActivity(intent);
		}
	}
}